#import "dfhgwhruebvhrheycerealreside.h"
#import "dfhgwhruebvhrheyroutediscard.h"
#import "dfhgwhruebvhrheyrelationshipgarbage.h"
#import "dfhgwhruebvhrheydispositionvisualizeViewController.h"
@interface dfhgwhruebvhrheydispositionvisualizeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeightConstraint;
@property (nonatomic, strong) dfhgwhruebvhrheycerealreside *cityheader;
@property (nonatomic, strong) dfhgwhruebvhrheyroutediscard *areaHeader;
@property (weak, nonatomic) IBOutlet UITableView *tabIV;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UIView *topView;
@end
static NSString *cellIdentifier = @"local_address_cell";
@implementation dfhgwhruebvhrheydispositionvisualizeViewController
- (dfhgwhruebvhrheycerealreside *)cityheader {
	if (_cityheader == nil) {
		_cityheader = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheycerealreside" owner:nil options:nil].firstObject;
		_cityheader.backgroundColor = @"#FAFAFA".dhgfywegwygeydwlearneddesignate;
	}
	return _cityheader;
}
- (dfhgwhruebvhrheyroutediscard *)areaHeader {
	if (_areaHeader == nil) {
		_areaHeader = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyroutediscard" owner:nil options:nil].firstObject;
		_areaHeader.backgroundColor = @"#FAFAFA".dhgfywegwygeydwlearneddesignate;
	}
	return _areaHeader;
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadUI];
	[self loadLocalAddressData];
	self.topView.hidden = !(self.level == 1);
	self.topViewHeightConstraint.constant = self.level == 1 ? 50 : 0;
}
- (void)loadUI {
	self.navigationItem.title = @"Address";
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	if (self.level == 2) {
		__weak typeof(self) weakSelf = self;
		self.cityheader.provice = self.provice;
		self.tabIV.tableHeaderView = self.cityheader;
		[self.cityheader resetActionCallBack:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[strongSelf headerResetAction];
		}];
		[self.cityheader mas_makeConstraints:^(MASConstraintMaker *make) {
			make.width.equalTo(self.view);
		}];
	}
	if (self.level == 3) {
		__weak typeof(self) weakSelf = self;
		self.areaHeader.city = self.city;
		self.areaHeader.provice = self.provice;
		self.tabIV.tableHeaderView = self.areaHeader;
		[self.areaHeader resetActionCallBack:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[strongSelf headerResetAction];
		}];
		[self.areaHeader mas_makeConstraints:^(MASConstraintMaker *make) {
			make.width.equalTo(self.view);
		}];
	}
	self.tabIV.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.tabIV registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
}
- (void)headerResetAction {
	[self.navigationController popViewControllerAnimated:YES];
}
- (void)loadLocalAddressData {
	self.dataArray = [@[] mutableCopy];
	NSString *path = [[NSBundle mainBundle] pathForResource:@"address_data" ofType:nil];
	NSData *jsonData = [NSData dataWithContentsOfFile:path];
	NSDictionary *root = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
	if (self.level == 1) {
		NSDictionary *proviceDic = root[@"data"];
		NSMutableArray *prefixs = [@[] mutableCopy];
		NSArray *provices = [proviceDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
		for (NSString *provice in provices) {
			NSString *prefix = [[provice substringToIndex:1] uppercaseString];
			if (![prefixs containsObject:prefix]) {
				[prefixs addObject:prefix];
			}
		}
		for (NSString * sectionkey in prefixs) {
			NSMutableArray *sections = [@[] mutableCopy];
			for (NSString *provice in provices) {
				if ([[[provice substringToIndex:1] uppercaseString] isEqualToString:sectionkey]) {
					dfhgwhruebvhrheyrelationshipgarbage *_provice = [[dfhgwhruebvhrheyrelationshipgarbage alloc] init];
					_provice.provice = provice;
					[sections addObject:_provice];
				}
			}
			[self.dataArray addObject:sections];
		}
	}
	if (self.level == 2) {
		NSDictionary *proviceDic = root[@"data"];
		if (![proviceDic.allKeys containsObject:self.provice]) {
			return;
		}
		NSDictionary *cityDic = root[@"data"][self.provice];
		NSMutableArray *prefixs = [@[] mutableCopy];
		NSArray *cities = [cityDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
		for (NSString *city in cities) {
			NSString *prefix = [[city substringToIndex:1] uppercaseString];
			if (![prefixs containsObject:prefix]) {
				[prefixs addObject:prefix];
			}
		}
		for (NSString * sectionkey in prefixs) {
			NSMutableArray *sections = [@[] mutableCopy];
			for (NSString *city in cities) {
				if ([[[city substringToIndex:1] uppercaseString] isEqualToString:sectionkey]) {
					dhgfywegwygeydwvictimminimal *_city = [[dhgfywegwygeydwvictimminimal alloc] init];
					_city.city = city;
					[sections addObject:_city];
				}
			}
			[self.dataArray addObject:sections];
		}
	}
	if (self.level == 3) {
		NSDictionary *proviceDic = root[@"data"];
		if (![proviceDic.allKeys containsObject:self.provice]) {
			return;
		}
		NSDictionary *cityDic = root[@"data"][self.provice];
		if (![cityDic.allKeys containsObject:self.city]) {
			return;
		}
		NSDictionary *areaDic = root[@"data"][self.provice][self.city];
		NSMutableArray *prefixs = [@[] mutableCopy];
		NSArray *areas = [areaDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
		for (NSString *area in areas) {
			NSString *prefix = [[area substringToIndex:1] uppercaseString];
			if (![prefixs containsObject:prefix]) {
				[prefixs addObject:prefix];
			}
		}
		for (NSString * sectionkey in prefixs) {
			NSMutableArray *sections = [@[] mutableCopy];
			for (NSString *area in areas) {
				if ([[[area substringToIndex:1] uppercaseString] isEqualToString:sectionkey]) {
					dhgfywegwygeydwminimalfantasy *_area = [[dhgfywegwygeydwminimalfantasy alloc] init];
					_area.area = area;
					[sections addObject:_area];
				}
			}
			[self.dataArray addObject:sections];
		}
	}
}
#pragma mark : ============= UITableViewDelegate ===========
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (self.level == 1) {
		dfhgwhruebvhrheydispositionvisualizeViewController *address = (dfhgwhruebvhrheydispositionvisualizeViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"address"];
		NSArray *sections = self.dataArray[indexPath.section];
		dfhgwhruebvhrheyrelationshipgarbage *provice = sections[indexPath.row];
		address.provice = provice.provice;
		address.origin = self.origin;
		address.level = 2;
		address.callback = self.callback;
		[self.navigationController pushViewController:address animated:YES];
		[[NSUserDefaults standardUserDefaults] setObject:provice.provice forKey:dhgfywegwygeydwfantasyviolin];
	}
	if (self.level == 2) {
		dfhgwhruebvhrheydispositionvisualizeViewController *address = (dfhgwhruebvhrheydispositionvisualizeViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"address"];
		NSArray *sections = self.dataArray[indexPath.section];
		dhgfywegwygeydwvictimminimal *city = sections[indexPath.row];
		address.provice = self.provice;
		address.origin = self.origin;
		address.city = city.city;
		address.level = 3;
		address.callback = self.callback;
		[self.navigationController pushViewController:address animated:YES];
		[[NSUserDefaults standardUserDefaults] setObject:city.city forKey:dhgfywegwygeydwviolinnamely];
	}
	if (self.level == 3) {
		NSArray *sections = self.dataArray[indexPath.section];
		dhgfywegwygeydwminimalfantasy *area = sections[indexPath.row];
		[[NSUserDefaults standardUserDefaults] setObject:area.area forKey:dhgfywegwygeydwnamelyprevalent];
		NSUInteger idx = [self.navigationController.viewControllers indexOfObject:self.origin];
		if (idx != NSNotFound) {
			if (self.callback) {
				self.callback(YES);
			}
			[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:idx] animated:YES];
		}
	}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 35;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	NSString *title = @"";
	NSArray *sections = self.dataArray[section];
	if (sections.count) {
		if (self.level == 1) {
			dfhgwhruebvhrheyrelationshipgarbage *provice = sections.firstObject;
			title = [[provice.provice substringToIndex:1] uppercaseString];
		}
		if (self.level == 2) {
			dhgfywegwygeydwvictimminimal *city = sections.firstObject;
			title = [[city.city substringToIndex:1] uppercaseString];
		}
		if (self.level == 3) {
			dhgfywegwygeydwminimalfantasy *area = sections.firstObject;
			title = [[area.area substringToIndex:1] uppercaseString];
		}
	}
	CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
	UIView *sectionView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, dhgfywegwygeydwdispositionvisualize, height)];
	UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(13, 0, dhgfywegwygeydwdispositionvisualize-26, height)];
	sectionView.backgroundColor = @"#FAFAFA".dhgfywegwygeydwlearneddesignate;
	titleLabel.backgroundColor = [UIColor clearColor];
	titleLabel.textColor = @"#AAAAAA".dhgfywegwygeydwlearneddesignate;
	titleLabel.font = [UIFont regular:14];
	[sectionView addSubview:titleLabel];
	titleLabel.text = title;
	return sectionView;
}
#pragma mark : ============= UITableViewDataSource ==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.dataArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
	if (self.level == 1) {
		dfhgwhruebvhrheyrelationshipgarbage *model = self.dataArray[indexPath.section][indexPath.row];
		cell.textLabel.text = model.provice;
	}
	if (self.level == 2) {
		dhgfywegwygeydwvictimminimal *model = self.dataArray[indexPath.section][indexPath.row];
		cell.textLabel.text = model.city;
	}
	if (self.level == 3) {
		dhgfywegwygeydwminimalfantasy *model = self.dataArray[indexPath.section][indexPath.row];
		cell.textLabel.text = model.area;
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.textLabel.textColor = @"#3B414B".dhgfywegwygeydwlearneddesignate;
	cell.textLabel.font = [UIFont regular:15];
	return cell;
}
@end
